﻿#include "RSCG12864B.h"

RAYLIDLCD::RAYLIDLCD(U16 BUSYPIN)
{
	_BUSYPIN = BUSYPIN;
}

void RAYLIDLCD::RAYLIDLCD::begin()
{
	Wire.begin();
	pinMode(_BUSYPIN, INPUT);
}

void RAYLIDLCD::clear()
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x10);
	Wire.endTransmission();
}

void RAYLIDLCD::displayOn()
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x11);
	Wire.endTransmission();
}

void RAYLIDLCD::displayOff()
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x12);
	Wire.endTransmission();
}

void RAYLIDLCD::setBrightness(U8 n)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x13);
	Wire.write(n);
	Wire.endTransmission();
}

void RAYLIDLCD::drawPixel(U8 x, U8 y)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x30);
	Wire.write(x);
	Wire.write(y);
	Wire.endTransmission();
}

void RAYLIDLCD::deletePixel(U8 x, U8 y)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x31);
	Wire.write(x);
	Wire.write(y);
	Wire.endTransmission();
}

void RAYLIDLCD::drawLine(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x32);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();
}

void RAYLIDLCD::deleteLine(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x33);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();
}

void RAYLIDLCD::drawRect(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x34);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();
}

void RAYLIDLCD::deleteRect(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x35);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();	
}

void RAYLIDLCD::drawRectF(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x36);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();	
}

void RAYLIDLCD::deleteRectF(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x37);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();	
}

void RAYLIDLCD::drawCircle(U8 x, U8 y, U8 r)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x38);
	Wire.write(x);
	Wire.write(y);
	Wire.write(r);
	Wire.endTransmission();	
}

void RAYLIDLCD::deleteCircle(U8 x, U8 y, U8 r)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x39);
	Wire.write(x);
	Wire.write(y);
	Wire.write(r);
	Wire.endTransmission();	
}

void RAYLIDLCD::drawCircleF(U8 x, U8 y, U8 r)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x3A);
	Wire.write(x);
	Wire.write(y);
	Wire.write(r);
	Wire.endTransmission();	
}

void RAYLIDLCD::deleteCircleF(U8 x, U8 y, U8 r)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x3B);
	Wire.write(x);
	Wire.write(y);
	Wire.write(r);
	Wire.endTransmission();
}

void RAYLIDLCD::inverselyArea(U8 x1, U8 y1, U8 x2, U8 y2)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x38);
	Wire.write(x1);
	Wire.write(y1);
	Wire.write(x2);
	Wire.write(y2);
	Wire.endTransmission();	
}

void RAYLIDLCD::displayBMP(U16 n)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x3D);
	Wire.write(n >> 8);
	Wire.write(n);
	Wire.endTransmission();
}

void RAYLIDLCD::displayBMPB(U16 n, U8 px, U8 py, U8 x, U8 y, U8 w, U8 h)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x3E);
	Wire.write(n >> 8);
	Wire.write(n);
	Wire.write(x);
	Wire.write(y);
	Wire.write(px);
	Wire.write(py);
	Wire.write(w);
	Wire.write(h);
	Wire.endTransmission();
}

void RAYLIDLCD::cursor(U8 x, U8 y)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x20);
	Wire.write(x);
	Wire.write(y);
	Wire.endTransmission();
}

void RAYLIDLCD::spacing(U8 xs, U8 ys)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x21);
	Wire.write(xs);
	Wire.write(ys);
	Wire.endTransmission();
}

void RAYLIDLCD::fontReversOn()
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x23);
	Wire.endTransmission();
}

void RAYLIDLCD::fontReversOff()
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x22);
	Wire.endTransmission();
}

void RAYLIDLCD::write(U8 x, U8 y, U32 n, U16 font)
{
	unsigned long i, temp;
	unsigned char t = 0;

	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x20);
	Wire.write(x);
	Wire.write(y);

	if (font == SMALL)
		Wire.write(0x24);
	else if (font == NORMAL)
		Wire.write(0x25);
	else if (font == BIG)
		Wire.write(0x26);

	if (n == 0)
	{
		Wire.write(0x30);
	}
	else
	{
		for (i = 1000000000; i >= 1; i = i / 10)
		{
			temp = n / i;
			if (t == 0 && temp)
			{
				Wire.write(temp % 10 + 0x30);
				t = 1;
			}
			else if (t == 1)
			{
				Wire.write(temp % 10 + 0x30);
			}
		}
	}
	Wire.write(0x00);
	Wire.endTransmission();
}

void RAYLIDLCD::print(U8 x, U8 y, CCHAR *ptr, U16 font)
{
	while (digitalRead(_BUSYPIN));
	Wire.beginTransmission(0x00);
	Wire.write(0x20);
	Wire.write(x);
	Wire.write(y);

	if (font == SMALL)
		Wire.write(0x24);
	else if (font == NORMAL)
		Wire.write(0x25);
	else if (font == BIG)
		Wire.write(0x26);
	else if (font == LARGE)
		Wire.write(0x27);
	else if (font == VLARGE)
		Wire.write(0x28);

	while (*ptr != '\0')
	{
		Wire.write(*ptr++);
	}
	Wire.write(0x00);
	Wire.endTransmission();
}